﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using HT.DAL;

namespace BLL
{
    public class HykblHBLL
    {
        private readonly Dal_HykblH_1 dal = new Dal_HykblH_1();

        public string WebApi { get; set; }
        public HykblHBLL(string webApi)
        {
            this.WebApi = webApi;
        }

        #region  Method



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(HykblHModel model)
        {
            return dal.Add(model);
        }


        public int GetNewDjlsh()
        {
            return dal.GetNewDjlsh( );
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string AddHykblH(HykblHModel model)
        {
            decimal sffsdx = 0;
            decimal zjxfsj = 0;
            decimal zjxfjf = 0;
            decimal jfye = 0;
            decimal xfzjf = 0;
            decimal hykye = 0;
            decimal hyzk = 0;
            if (model.sffsdx != null && model.sffsdx.ToString() != "")
            {
                sffsdx = decimal.Parse(model.sffsdx.ToString());
            }
            if (model.zjxfsj != null && model.zjxfsj.ToString() != "")
            {
                zjxfsj = decimal.Parse(model.zjxfsj.ToString());
            }
            if (model.zjxfjf != null && model.zjxfjf.ToString() != "")
            {
                zjxfjf = decimal.Parse(model.zjxfjf.ToString());
            }
            if (model.jfye != null && model.jfye.ToString() != "")
            {
                jfye = decimal.Parse(model.jfye.ToString());
            }

            if (model.xfzjf != null && model.xfzjf.ToString() != "")
            {
                xfzjf = decimal.Parse(model.xfzjf.ToString());

            }
            if (model.hykye != null && model.hykye.ToString() != "")
            {
                hykye = decimal.Parse(model.hykye.ToString());
            }

            if (model.hyzk != null && model.hyzk.ToString() != "")
            {
                hyzk = decimal.Parse(model.hyzk.ToString());
            }


            //DjBtZdh,DjFtZdh,DjStZdh,DjState,DjCount
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into hykblH(");
            strSql.Append("DjLsh,dmbh,dmmc,khbh,khmc,kh,dh,dz,hyzk,hykye,xfzjf,jfye,zjxfjf,zjxfsj,hykzt,sffsdx,rq,sj,djyh,djsj,xgyh,mm,xgsj,bz,srmm,cfsrmm,sfz,sr,xb,ykh)");
            strSql.Append(" values (");
            strSql.Append(model.DjLsh + ",'" + model.dmbh + "','" + model.dmmc + "','" + model.khbh + "','" + model.khmc + "','" + model.kh + "','" + model.dh + "','" + model.dz + "'," + hyzk + "," + hykye + "," + xfzjf + "," + jfye + "," + zjxfjf + "," + zjxfsj + ",'" + model.hykzt + "'," + sffsdx + ",'" + model.rq + "','" + model.sj + "','" + model.djyh + "','" + model.djsj + "','" + model.xgyh + "','" + model.mm + "','" + model.xgsj + "','" + model.bz + "','" + model.srmm + "','" + model.cfsrmm + "','" + model.sfz + "','" + model.sr + "','" + model.xb + "','" + model.ykh + "')");

            return strSql.ToString();
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(HykblHModel model)
        {
            return dal.Update(model);
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public string UpdateHykblH(HykblHModel model)
        {
            //DjBtZdh=,DjFtZdh=,DjStZdh=,

            decimal sffsdx = 0;
            decimal zjxfsj = 0;
            decimal zjxfjf = 0;
            decimal jfye = 0;
            decimal xfzjf = 0;
            decimal hykye = 0;
            decimal hyzk = 0;
            if (model.sffsdx != null && model.sffsdx.ToString() != "")
            {
                sffsdx = decimal.Parse(model.sffsdx.ToString());
            }
            if (model.zjxfsj != null && model.zjxfsj.ToString() != "")
            {
                zjxfsj = decimal.Parse(model.zjxfsj.ToString());
            }
            if (model.zjxfjf != null && model.zjxfjf.ToString() != "")
            {
                zjxfjf = decimal.Parse(model.zjxfjf.ToString());
            }
            if (model.jfye != null && model.jfye.ToString() != "")
            {
                jfye = decimal.Parse(model.jfye.ToString());
            }

            if (model.xfzjf != null && model.xfzjf.ToString() != "")
            {
                xfzjf = decimal.Parse(model.xfzjf.ToString());

            }
            if (model.hykye != null && model.hykye.ToString() != "")
            {
                hykye = decimal.Parse(model.hykye.ToString());
            }

            if (model.hyzk != null && model.hyzk.ToString() != "")
            {
                hyzk = decimal.Parse(model.hyzk.ToString());
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update hykblH set ");
            strSql.Append("dh='" + model.dh + "',");
            strSql.Append("dz='" + model.dz + "',");
            strSql.Append("hyzk=" + hyzk + ",");
            strSql.Append("hykye=" + hykye + ",");
            strSql.Append("xfzjf=" + xfzjf + ",");
            strSql.Append("jfye=" + jfye + ",");
            strSql.Append("zjxfjf=" + zjxfjf + ",");
            strSql.Append("zjxfsj=" + zjxfsj + ",");
            strSql.Append("hykzt='" + model.hykzt + "',");
            strSql.Append("sffsdx=" + sffsdx + ",");
            strSql.Append("rq='" + model.rq + "',");
            strSql.Append("sj='" + model.sj + "',");
            strSql.Append("djyh='" + model.djyh + "',");
            strSql.Append("djsj='" + model.djsj + "',");
            strSql.Append("xgyh='" + model.xgyh + "',");
            strSql.Append("mm='" + model.mm + "',");
            strSql.Append("xgsj='" + model.xgsj + "',");
            strSql.Append("bz='" + model.bz + "',");
            strSql.Append("srmm='" + model.srmm + "',");
            strSql.Append("cfsrmm='" + model.cfsrmm + "',");
            strSql.Append("sfz='" + model.sfz + "',");
            strSql.Append("sr='" + model.sr + "',");
            strSql.Append("xb='" + model.xb + "',");
            strSql.Append("ykh='" + model.ykh + "'");
            strSql.Append(" where   kh='" + model.kh + "'");

            return strSql.ToString();
        }



        public int AddOrUpdateModel(HykblHModel hykblModel)
        {
            int ss = 0;


            if (hykblModel != null)
            {
                string kh = hykblModel.kh;

                if (IsExistHykblH(kh))
                {
                    //hykblModel.djyh = "";
                    //hykblModel.djsj = DateTime.Parse("1900-1-1");
                    ss = UpdateHykblHModel(hykblModel);
                }
                else
                {

                    int djlsh = GetNewDjlsh();

                    hykblModel.DjLsh = djlsh;
                    hykblModel.xgyh = "";
                    hykblModel.xgsj = DateTime.Parse("1900-1-1");
                    ss = AddHykblHModel(hykblModel);
                }

            }

            return ss;
        }




        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int AddHykblHModel(HykblHModel model)
        {
            return dal.AddHykblHModel(model);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int UpdateHykblHModel(HykblHModel model)
        {
            return dal.UpdateHykblHModel(model);
        }
        /// <summary>
        /// 会员卡号是否存在
        /// </summary>
        /// <param name="kh"></param>
        /// <returns></returns>
        public bool IsExistHykblH(string kh)
        {
            return dal.IsExistHykblH(kh);
        }


        public string InsertHyczH(string kh, string je, string dmbh, string yyy)
        {
            string sj = DateTime.Now.ToString();
            string sql = "insert into hykczjl(Hykh,Sj,Je,Dmbh,Yyy) values('" + kh + "','" + sj + "'," + je + ",'" + dmbh + "','" + yyy + "')";
            return sql;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete()
        {
            return dal.Delete( );
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HykblHModel GetModel(string kh)
        {
            return dal.GetModel(kh);
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HykblHModel GetModel(string kh, string dmbh)
        {
            return dal.GetModel(kh,   dmbh);
        }


        public int HykCZ(string kh, decimal je, string dmbh, string yyybh)
        {
            return dal.HykCZ(kh,   je,   dmbh,   yyybh);
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }



        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }



        public DataSet GetHykblHData()
        {
            return dal.GetHykblHData( );
        }
        public DataSet GetHykczjlData()
        {
            return dal.GetHykczjlData( );
        }
        public string UpdateHykblH(string kh)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update hykblH set ");
            strSql.Append("UploadStatus='1' ");
            strSql.Append(" where   kh='" + kh + "'");
            return strSql.ToString();
        }
        public string UpdateHykczjl(string id)
        {
            StringBuilder strSql = new StringBuilder();
            //select *from  dbo.hykczjl  where id=4


            strSql.Append("update hykczjl set ");
            strSql.Append("UploadStatus='1' ");
            strSql.Append(" where   id= " + id);
            return strSql.ToString();
        }


        public int ExecuteSqlTran(List<string> sqlList)
        {
            return dal.ExecuteSqlTran(sqlList);
        }
        public int SaveHykblHData(List<string> sqlList)
        {
            return dal.SaveHykblHData(sqlList);
        }



        #endregion  Method
        #region WebApi操作

        /// <summary>
        /// 获取会员信息
        /// </summary>
        /// <param name="kh"></param>
        /// <param name="dmbh"></param>
        /// <returns></returns>
        public hykblH_Entity GetHykblHModelByWebApi(string kh, string dmbh)
        {
            Uri _baseAddress = new Uri(this.WebApi);

            Uri _address = new Uri(_baseAddress, "/api/GetHykblHModel/?kh=" + kh + "&dmbh=" + dmbh);
            string reslut = HttpGet(_address.AbsoluteUri);

            hykblH_Entity entity = JsonConvert.DeserializeObject<hykblH_Entity>(reslut);
            return entity;
        }
        /// <summary>
        /// 会员卡 充值
        /// </summary>
        /// <param name="kh"></param>
        /// <param name="je"></param>
        /// <param name="dmbh"></param>
        /// <param name="yyybh"></param>
        /// <returns></returns>
        public int HykCZByWebApi(string kh, decimal je, string dmbh, string yyybh)
        {
            Uri _baseAddress = new Uri(this.WebApi);
            Uri _address = new Uri(_baseAddress, "/api/GetHykCZ/?kh=" + kh + "&je=" + je + "&dmbh=" + dmbh + "&yyybh=" + yyybh);
            string reslut = HttpGet(_address.AbsoluteUri);

            DOT entity = JsonConvert.DeserializeObject<DOT>(reslut);


            if (entity != null)
                return entity.Status;
            else
                return 0;
        }

        /// <summary>
        /// 会员卡 添加/修改
        /// </summary>
        /// <param name="hykblModel"></param>
        /// <returns></returns>
        public int HykAddByWebApi(hykblH_Entity hykblModel)
        {
            Uri _baseAddress = new Uri(this.WebApi);
            Uri _address = new Uri(_baseAddress, "/api/PostHykModel");
            //  string reslut = HttpGet(_address.AbsoluteUri);
            string postData = JsonConvert.SerializeObject(hykblModel);
            string reslut = HttpPost(_address.AbsoluteUri, postData);


            DOT entity = JsonConvert.DeserializeObject<DOT>(reslut);


            if (entity != null)
                return entity.Status;
            else
                return 0;
        }

        /// <summary>
        /// 会员卡 删除
        /// </summary>
        /// <param name="hks"></param>
        /// <returns></returns>
        public int DeleteHykblByWebApi(string kh)
        {
            Uri _baseAddress = new Uri(this.WebApi);
            Uri _address = new Uri(_baseAddress, "/api/GetDeleteHykbl/?kh=" + kh);
            string reslut = HttpGet(_address.AbsoluteUri);

            DOT entity = JsonConvert.DeserializeObject<DOT>(reslut);


            if (entity != null)
                return entity.Status;
            else
                return 0;
        }

        public int HykblCount0(string dmbh)
        {
            Uri _baseAddress = new Uri(this.WebApi);
            Uri _address = new Uri(_baseAddress, "/api/GetHykblCount0/?dmbh=" + dmbh);

            string reslut = HttpGet(_address.AbsoluteUri);

            DOT entity = JsonConvert.DeserializeObject<DOT>(reslut);


            if (entity != null)
                return entity.Status;
            else
                return 0;
        }
        public int HykblCount1(string dmbh, string value, string time1, string time2)
        {
            Uri _baseAddress = new Uri(this.WebApi);
            Uri _address = new Uri(_baseAddress, "/api/GetHykblCount1/?dmbh=" + dmbh + "&value=" + value + "&time1=" + time1 + "&time2=" + time2);

            string reslut = HttpGet(_address.AbsoluteUri);

            DOT entity = JsonConvert.DeserializeObject<DOT>(reslut);


            if (entity != null)
                return entity.Status;
            else
                return 0;
        }
        /// <summary>
        /// 会员卡消费
        /// </summary>
        /// <param name="hykye"></param>
        /// <param name="jfye"></param>
        /// <param name="zjxfjf"></param>
        /// <param name="time"></param>
        /// <param name="kh"></param>
        /// <returns></returns>
        public int GetHykXfByWebApi(string hykye, string jfye, string zjxfjf, string time, string kh)
        {
            Uri _baseAddress = new Uri(this.WebApi);
            Uri _address = new Uri(_baseAddress, "/api/GetHykXF/?hykye=" + hykye + "&jfye=" + jfye + "&zjxfjf=" + zjxfjf + "&time=" + time+ "&kh="+ kh);

            string reslut = HttpGet(_address.AbsoluteUri);
            DOT entity = JsonConvert.DeserializeObject<DOT>(reslut);


            if (entity != null)
                return entity.Status;
            else
                return 0;
        }

        public List<hykblH_Entity> GetHykblHList(string dmbh, string value, string time1, string time2, int pageIndex, int pageSize)
        {
            Uri _baseAddress = new Uri(this.WebApi);
            Uri _address = new Uri(_baseAddress, "/api/GetHykblHList/?dmbh=" + dmbh + "&value=" + value + "&time1=" + time1 + "&time2=" + time2 + "&pageIndex=" + pageIndex + "&pageSize=" + pageSize);
            string reslut = HttpGet(_address.AbsoluteUri);
            List<hykblH_Entity> ls = JsonConvert.DeserializeObject<List<hykblH_Entity>>(reslut);
            return ls;
        }
        public List<hykblH_Entity> GetHykblHList(string dmbh,int pageIndex, int pageSize)
        {
            Uri _baseAddress = new Uri(this.WebApi);
            Uri _address = new Uri(_baseAddress, "/api/GetHykblHInitList/?dmbh=" + dmbh + "&pageIndex=" + pageIndex + "&pageSize=" + pageSize);
            string reslut = HttpGet(_address.AbsoluteUri);
            List<hykblH_Entity> ls = JsonConvert.DeserializeObject<List<hykblH_Entity>>(reslut);
            return ls;
        }




        public static string HttpPost(string url, string body)
        {
            //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Accept = "application/json";
            request.ContentType = "application/json";

            byte[] buffer = encoding.GetBytes(body);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }



        public string HttpGet(string url)
        {
            //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "application/json";
            request.ContentType = "application/json";
            //  client.Headers["Accept"] = 
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

        #endregion
    }
}
